﻿using System;
using System.Runtime.InteropServices;
using System.Text;
using System.ComponentModel;

namespace Yak
{

    public static class NativeMethods
    {

        [DllImport("kernel32", SetLastError = true, CharSet=CharSet.Unicode)]
        private static extern int WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        internal static void WriteValue(String INIPath, String Section, String Key, String Value)
        {
            if (WritePrivateProfileString(Section, Key, Value, INIPath) == 0)
                throw new Exception(new Win32Exception(Marshal.GetLastWin32Error()).Message);
        }

        internal static String ReadValue(String INIPath, String Section, String Key)
        {
            StringBuilder OutBuffer = new StringBuilder(255);
            int ReturnValue = GetPrivateProfileString(Section,Key,null,OutBuffer,255,INIPath);
            if (ReturnValue == 0)
                throw new Exception(new Win32Exception(Marshal.GetLastWin32Error()).Message);
            else
                return OutBuffer.ToString();
        }
    }
}